﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.IO;

public partial class Admin_AdminProductAppleTVManager : System.Web.UI.Page
{
    private string connectionString;
    private SqlConnection connect;
    private SqlDataAdapter da;
    private SqlCommand command;
    private DataSet ds;
    private SqlTransaction transaction;
    private string image="";
    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString = WebConfigurationManager.ConnectionStrings["AppleShopConnectionString"].ToString();
        connect = new SqlConnection(connectionString);
        command = new SqlCommand();
        string status = Request.QueryString["Status"];
        if (status == "Update")
        {
            txtNo.Text = Request.QueryString["No"];
            txtDetails.Text = Request.QueryString["ID"];
            imgDetails.ImageUrl = Request.QueryString["Img"];
            btnUpdate.Text = "Update";
            txtDetails.Enabled = false;
            fileupImg.Visible = true;
        }
        else 
        {
            txtDetails.Enabled = true;
            fileupImg.Visible = true;
            imgDetails.Visible = false;
            btnUpdate.Text = "Insert";
        }
    }

    protected bool TransactionUpdate(int no, string img)
    {

        if (connect.State == ConnectionState.Open)
        {
            connect.Close();
        }
        connect.Open();
        transaction = connect.BeginTransaction();    //bắt đầu quá trình quản lý transaction

        command.Transaction = transaction;
        try
        {
            //new SqlCommand(query, connect, transaction).ExecuteNonQuery();
            SqlCommand cmd = new SqlCommand("spUpdateProductAppleTVDetails", connect, transaction);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@No", SqlDbType.Int).Value = no;
            cmd.Parameters.Add("@Image", SqlDbType.VarChar).Value = img;
            cmd.ExecuteNonQuery();
            transaction.Commit();
            connect.Close();
            return true;
        }
        catch (Exception ex)
        {
            transaction.Rollback();
            connect.Close();
            return false;
        }
    }

    protected bool TransactionInsert(string img)
    {

        if (connect.State == ConnectionState.Open)
        {
            connect.Close();
        }
        connect.Open();
        transaction = connect.BeginTransaction();    //bắt đầu quá trình quản lý transaction

        command.Transaction = transaction;
        try
        {
            //new SqlCommand(query, connect, transaction).ExecuteNonQuery();
            SqlCommand cmd = new SqlCommand("spInsertNewAppleTVDetails", connect, transaction);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Image", SqlDbType.VarChar).Value = img;
            cmd.ExecuteNonQuery();
            transaction.Commit();
            connect.Close();
            return true;
        }
        catch (Exception ex)
        {
            transaction.Rollback();
            connect.Close();
            return false;
        }
    }

    protected bool checkImg()
    {
        bool check = false;
        if (fileupImg.HasFile)
        {
            string[] validate = { "bmp", "jpg", "jpeg", "png", "gif" };
            string ext = Path.GetExtension(fileupImg.PostedFile.FileName);

            for (int i = 0; i < validate.Length; i++)
            {
                if (ext == "." + validate[i])
                {
                    if (fileupImg.PostedFile.ContentLength <= 5120000)
                    {
                        check = true;
                        break;
                    }
                }
            }
            if (check)
            {
                string filename = Path.GetFileName(fileupImg.FileName);
                fileupImg.SaveAs(Server.MapPath("~/IMG/AppleTV/") + filename);
                //lblStatus.Text = "Successfully";
                image = "~/IMG/AppleTV/" + filename;
                //lblStatus.Visible = true;


            }
            else
            {
                //lblStatus.Text = "File must be Images and less than 500kb";
                //lblStatus.Visible = true;
            }
        }
        return check;
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        //string img = image;
        if (btnUpdate.Text == "Update")
        {
            int No = Int32.Parse(txtNo.Text);
            if (checkImg())
            {
                if (TransactionUpdate(No, image))
                {
                    //ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('" + "Update Successful" + "');", true);
                    Response.Redirect("AdminProductAppleTV.aspx");
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('" + "Update Failed" + "');", true);
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('" + "Update failed" + "');", true);
            }
        }
        else
        {
            if (checkImg())
            {
                if (TransactionInsert(image))
                {
                    //ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('" + "Update Successful" + "');", true);
                    Response.Redirect("AdminProductAppleTV.aspx");
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('" + "Insert Failed" + "');", true);
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('" + "Insert Image failed" + "');", true);
            }
        }
    }
}